<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('定时任务列表')" />
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="job-form">
					<div class="select-list">
						<ul>
							<li>
								任务类名：<input type="text" name="jobName"/>
							</li>
							<li>
								任务状态：<select name="status" th:with="type=${@dict.getType('quartz_status')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.itemText}" th:value="${dict.itemValue}"></option>
								</select>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			
			<div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="quartz:job:add">
	                <i class="fa fa-plus"></i> 新增
	            </a>
	            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="quartz:job:edit">
		            <i class="fa fa-edit"></i> 修改
		        </a>
				<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="quartz:job:remove">
		            <i class="fa fa-remove"></i> 删除
		        </a>
				<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="quartz:job:export">
		            <i class="fa fa-download"></i> 导出
		        </a>
	        </div>
        
	        <div class="col-sm-12 select-table table-striped">
			    <table id="bootstrap-table"></table>
			</div>
		</div>
	</div>
	<th:block th:include="include :: footer" />
	<script th:inline="javascript">
		var editFlag = [[${@permission.hasPermi('quartz:job:edit')}]];
		var removeFlag = [[${@permission.hasPermi('quartz:job:remove')}]];
		var statusFlag = [[${@permission.hasPermi('quartz:job:changeStatus')}]];
		var prefix = ctx + "sys/quartzJob";

		$(function() {
		    var options = {
		        url: prefix + "/list",
		        createUrl: prefix + "/add",
		        updateUrl: prefix + "/edit/{id}",
		        removeUrl: prefix + "/deleteBatch",
		        exportUrl: prefix + "/exportXls",
		        sortName: "createTime",
		        sortOrder: "desc",
		        modalName: "任务",
		        columns: [{
		            checkbox: true
		        },
				{
					field: 'no',
					title: '序号',
					align: "center",
					formatter: function (value, row, index) {
						//获取每页显示的数量
						var pageSize = $('#bootstrap-table').bootstrapTable('getOptions').pageSize;
						//获取当前是第几页
						var pageNumber = $('#bootstrap-table').bootstrapTable('getOptions').pageNumber;
						//返回序号，注意index是从0开始的，所以要加上1
						return pageSize * (pageNumber - 1) + index + 1;
					}
				},
				{
					field: 'id',
					title: '任务编号',
					visible: false
				},
				{
		            field: 'jobClassName',
		            title: '任务类名',
		        },
				{
					field: 'cronExpression',
					title: 'cron表达式'
				},
				{
					field: 'parameter',
					title: '参数'
				},
				{
					field: 'description',
					title: '描述'
				},
		        {
		        	visible: statusFlag !== 'hidden',
		        	title: '任务状态',
		        	align: 'center',
		        	formatter: function (value, row, index) {
		        		return statusTools(row);
		        	}
		        },
		        {
		            field: 'createTime',
		            title: '创建时间',
		            sortable: true
		        },
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		                var actions = [];
		                actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
		                actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');
		                return actions.join('');
		            }
		        }]
		    };
		    $.table.init(options);
		});
        
		/* 调度任务状态显示 */
		function statusTools(row) {
		    if (row.status === -1) {
    			return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="start(\'' + row.jobClassName + '\')"></i> ';
    		} else {
    			return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="stop(\'' + row.jobClassName + '\')"></i> ';
    		}
		}

		/* 调度任务-停用 */
		function stop(jobClassName) {
			$.modal.confirm("确认要停用任务吗？", function() {
				$.operate.get(prefix + "/pause?jobClassName=" + jobClassName);
		    })
		}

		/* 调度任务-启用 */
		function start(jobClassName) {
			$.modal.confirm("确认要启用任务吗？", function() {
				$.operate.get(prefix + "/resume?jobClassName=" + jobClassName);
		    })
		}
	</script>
</body>
</html>